<?php
require_once 'objs/User.php';
session_start();
include_once "models/UserDao.php";
include_once "models/Util.php";
include_once "models/MySqlDataAccessHelper.php";

$msgError = NULL;
$leftContent = 'indexleft.php';

$user = Util::getSessionParam('user');
$act = Util::getRequestParam('act');
if ($user == NULL && $act == 'login') {
    $userName = Util::getRequestParam('username');
    $passwd = Util::getRequestParam('passwd');
    if (strlen($userName) > 0 && $userName != 'Email') {
        $md5Pass = md5($passwd);
        $user = UserDao::getUser($userName, $md5Pass);
        if ($user == NULL) {
            $msgError = 'Sai email hoặc mật khẩu!';
        }else{
            $_SESSION['user'] = $user;
        }
    } else {
        $msgError = 'Bạn hãy nhập email!';
    }
    if($msgError != NULL){
        echo "<script>location.href='dangnhap.php?error=$msgError'</script>";
    }
}else if($act == 'logout'){
    $user = NULL;
    Util::freeSessionParam('user');
    session_destroy();
}

include 'subs/template.php';
?>
